package org.msfj.hospital.service.impl;

import org.msfj.hospital.model.HospitalNews;
import org.msfj.hospital.vo.HospitalNewsVo;
import org.msfj.hospital.service.HospitalNewsService;
import org.msfj.common.base.BaseServiceImpl;
import org.msfj.common.annotation.Constants;
import org.msfj.common.enums.Enable;
import org.springframework.stereotype.Service;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.beans.BeanUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.collection.CollectionUtil;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
import java.util.ArrayList;

/**
 * @Description: 医院新闻信息service实现类
 * @Author: like
 * @Email: like@deyatong.com
 * @Date: 2018-08-21 15:01:20
 * @Version: 1.0
 * @Created in idea by autoCode
 */
@Service
@CacheConfig(cacheNames = Constants.CACHE_NAMESPACE + "hospitalNews")
public class  HospitalNewsServiceImpl extends BaseServiceImpl<HospitalNews> implements HospitalNewsService {

    /**
     * 根据条件分页查询医院新闻信息列表
     *
     * @param hospitalNewsVo
     * @param page 当前第几页
     * @param rows 每页多少条记录
     * @return PageInfo<HospitalNews>
     */
    @Override
    //@Cacheable
    public PageInfo<HospitalNewsVo> getByHospitalNews(HospitalNewsVo hospitalNewsVo, int page, int rows) {
        Example example = new Example(HospitalNews.class);
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        PageHelper.startPage(page, rows);
        List<HospitalNews> hospitalNewsList = super.getByExample(example);
        PageInfo hospitalNewsVoPageInfo = new PageInfo(hospitalNewsList);
        hospitalNewsVoPageInfo.setList(setOtherProperties(hospitalNewsList));
        return hospitalNewsVoPageInfo;
    }

    /**
     * 根据条件查询所有医院新闻信息对象列表
     *
     * @param hospitalNewsVo
     * @return List<HospitalNews>
     */
    @Override
    //@Cacheable
    public List<HospitalNewsVo> getAllHospitalNews(HospitalNewsVo hospitalNewsVo) {
        Example example = new Example(HospitalNews.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("enable", Enable.ENABLE.getCode());
        List<HospitalNewsVo> hospitalNewsVoList = setOtherProperties(super.getByExample(example));
        return hospitalNewsVoList;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param hospitalNews
     * @return DepartmentVo
     */
    @Override
    //@Cacheable
    public HospitalNewsVo setOtherProperties(HospitalNews hospitalNews) {
        HospitalNewsVo hospitalNewsVo = new HospitalNewsVo();
        BeanUtils.copyProperties(hospitalNews, hospitalNewsVo);
        return hospitalNewsVo;
    }

    /**
     * 设置vo的其他属性属性
     *
     * @param hospitalNewss
     * @return List<HospitalNewsVo>
    */
    @Override
    //@Cacheable
    public List<HospitalNewsVo> setOtherProperties(List<HospitalNews> hospitalNewss) {
        List<HospitalNewsVo> hospitalNewsVos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(hospitalNewss)) {
            for (HospitalNews hospitalNews : hospitalNewss) {
                HospitalNewsVo hospitalNewsVo = new HospitalNewsVo();
                BeanUtils.copyProperties(hospitalNews, hospitalNewsVo);
                hospitalNewsVos.add(hospitalNewsVo);
            }
        }
        return hospitalNewsVos;
    }
}
